/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.Reportes.Vista;

import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import pe.edu.pucp.Modelo.Ciudad;
import pe.edu.pucp.Modelo.Continente;
import pe.edu.pucp.Modelo.Pais;
import pe.edu.pucp.Modelo.Permiso;
import pe.edu.pucp.Util.Constante;
import pe.edu.pucp.Util.Reportclass.RepKardex;
/**
 *
 * @author jorge
 */
public class ReporteKardexGUI extends javax.swing.JPanel {

    private JFrame padre;
    /**
     * Creates new form ReportePedidosGUI
     */
    public ReporteKardexGUI() {
        initComponents();
        ((JTextField)dtcFechaInicio.getDateEditor()).setEditable(false);
        ((JTextField)dtcFechaFin.getDateEditor()).setEditable(false); 
        dtcFechaInicio.setMaxSelectableDate(new Date());
        dtcFechaFin.setMaxSelectableDate(new Date());           
    }
   
    public JFrame getPadre() { return padre; }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmbContinente = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cmbPais = new javax.swing.JComboBox();
        cmbCiudad = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        btnBuscar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        btnGenerarPDF = new javax.swing.JButton();
        dtcFechaFin = new com.toedter.calendar.JDateChooser();
        dtcFechaInicio = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        pnKardex = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResultados = new javax.swing.JTable();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        setMaximumSize(new java.awt.Dimension(764, 700));
        setMinimumSize(new java.awt.Dimension(764, 700));
        setName(""); // NOI18N
        setPreferredSize(new java.awt.Dimension(764, 700));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Reporte de Kardex"));
        jPanel1.setPreferredSize(new java.awt.Dimension(634, 240));

        jLabel1.setText("Continente:");

        jLabel3.setText("Pais:");

        jLabel5.setText("Ciudad:");

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/search.png"))); // NOI18N

        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/refresh.png"))); // NOI18N

        btnGenerarPDF.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/address_book.png"))); // NOI18N

        dtcFechaFin.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dtcFechaFinPropertyChange(evt);
            }
        });

        dtcFechaInicio.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dtcFechaInicioPropertyChange(evt);
            }
        });

        jLabel4.setText("Fecha Inicio: ");

        jLabel2.setText("Fecha Fin: ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmbContinente, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbPais, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dtcFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dtcFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 195, Short.MAX_VALUE)
                .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGenerarPDF, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(cmbContinente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(cmbPais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(btnGenerarPDF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnBuscar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnLimpiar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(cmbCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addGap(6, 6, 6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(dtcFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(dtcFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addContainerGap(38, Short.MAX_VALUE))))
        );

        pnKardex.setBorder(javax.swing.BorderFactory.createTitledBorder("Movimientos"));

        tblResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "FECHA", "APERTURA", "ENTRADAS", "SALIDAS", "CIERRE"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblResultados.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblResultados);
        tblResultados.getColumnModel().getColumn(0).setResizable(false);
        tblResultados.getColumnModel().getColumn(0).setPreferredWidth(200);
        tblResultados.getColumnModel().getColumn(1).setResizable(false);
        tblResultados.getColumnModel().getColumn(1).setPreferredWidth(125);
        tblResultados.getColumnModel().getColumn(2).setResizable(false);
        tblResultados.getColumnModel().getColumn(2).setPreferredWidth(125);
        tblResultados.getColumnModel().getColumn(3).setResizable(false);
        tblResultados.getColumnModel().getColumn(3).setPreferredWidth(125);
        tblResultados.getColumnModel().getColumn(4).setResizable(false);
        tblResultados.getColumnModel().getColumn(4).setPreferredWidth(125);

        javax.swing.GroupLayout pnKardexLayout = new javax.swing.GroupLayout(pnKardex);
        pnKardex.setLayout(pnKardexLayout);
        pnKardexLayout.setHorizontalGroup(
            pnKardexLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnKardexLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnKardexLayout.setVerticalGroup(
            pnKardexLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnKardexLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 736, Short.MAX_VALUE)
                    .addComponent(pnKardex, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnKardex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void dtcFechaInicioPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dtcFechaInicioPropertyChange
        if ("date".equals(evt.getPropertyName())) {
            dtcFechaFin.setMinSelectableDate((Date) evt.getNewValue());
        }    
    }//GEN-LAST:event_dtcFechaInicioPropertyChange

    private void dtcFechaFinPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dtcFechaFinPropertyChange
        if ("date".equals(evt.getPropertyName())) {
            dtcFechaInicio.setMaxSelectableDate((Date) evt.getNewValue());                              
        } 
    }//GEN-LAST:event_dtcFechaFinPropertyChange


    public Date getFechaFin(){ 
        return dtcFechaFin.getDate(); 
    }     
    
    public Date getFechaInicio(){ 
        return dtcFechaInicio.getDate(); 
    }         
    
    public int getContinente(){ 
        if (cmbContinente.getSelectedIndex() > 0)
            return cmbContinente.getSelectedIndex() - 1; 
        else return -1;
    }  

    public int getPais(){ 
        if (cmbPais.getSelectedIndex() > 0)
            return cmbPais.getSelectedIndex() - 1; 
        else return -1;
    }             
    
    public int getCiudad(){ 
        if (cmbCiudad.getSelectedIndex() > 0)
            return cmbCiudad.getSelectedIndex() - 1; 
        else return -1;
    } 
    
    public void mostrarPermisos(ArrayList<Permiso> list){            
        if (!list.isEmpty()){                                
            btnBuscar.setEnabled(buscarPermiso(list,Constante.P_REP_KAR_BUSCAR)==1);
            btnGenerarPDF.setEnabled(buscarPermiso(list,Constante.P_REP_KAR_GENERAR)==1);
        }
    }    
  
    public int buscarPermiso(ArrayList<Permiso> list, int idel){        
        for (Permiso opc : list){
            if (opc.getElemento().getIdelemento()==idel){ return opc.getPermiso(); }
        }
        return -1;
    }     
 
    public void addLimpiarListener(ActionListener listener) { btnLimpiar.addActionListener(listener); }
        
    public void addBuscarListener(ActionListener ml) { btnBuscar.addActionListener(ml); }
    
    public void addGenerarListener(ActionListener ml) { btnGenerarPDF.addActionListener(ml); }
    
    public void addCambiarContinente(ItemListener ml){ cmbContinente.addItemListener(ml); }     
    
    public void addCambiarPais(ItemListener ml){ cmbPais.addItemListener(ml); }  
    
    public void addCambiarCiudad(ItemListener ml){ cmbCiudad.addItemListener(ml); }  
 
   
    public void llenarComboContinente(ArrayList<Continente> lista){
        cmbContinente.removeAllItems();
        cmbContinente.addItem(Constante.DEFAULT_OPCION); 
        if(lista!=null){
            for (Continente opc : lista){
                cmbContinente.addItem(opc.getDescripcion());
            }
        }
    }    
    
    public void llenarComboPais(ArrayList<Pais> lista){
        cmbPais.removeAllItems();
        cmbPais.addItem(Constante.DEFAULT_OPCION); 
        if(lista!=null){           
            for (Pais opc : lista){
                cmbPais.addItem(opc.getDescripcion());
            }
        }
    }    

    public void llenarComboCiudad(ArrayList<Ciudad> lista){
        cmbCiudad.removeAllItems();
        cmbCiudad.addItem(Constante.DEFAULT_OPCION); 
        if(lista!=null){
            for (Ciudad opc : lista){
                cmbCiudad.addItem(opc.getDescripcion());
            }
        }
    }        
          
    public void llenaGrilla(ArrayList<RepKardex> lista){        
        if(lista!=null){ 
            ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
            for (int f = 0; f<lista.size();f++){
                ((DefaultTableModel) tblResultados.getModel()).setRowCount(tblResultados.getRowCount()+1);               
                tblResultados.setValueAt(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(lista.get(f).getFechaReg()),f,0);
                tblResultados.setValueAt(lista.get(f).getApertura(),f,1);
                tblResultados.setValueAt(lista.get(f).getPaqentrantes(),f,2);                
                tblResultados.setValueAt(lista.get(f).getPaqsalientes(),f,3);                
                tblResultados.setValueAt(lista.get(f).getCierre(),f,4);                
            }
            pnKardex.setBorder(javax.swing.BorderFactory.createTitledBorder("Movimientos ("+lista.size() + ")"));
        } 
        else {
            pnKardex.setBorder(javax.swing.BorderFactory.createTitledBorder("Movimientos"));
        }
    }
    
    public void LimpiarDatos(){
        cmbContinente.setSelectedIndex(0);
        cmbPais.setSelectedIndex(0);
        cmbCiudad.setSelectedIndex(0);
        dtcFechaInicio.setDate(null);
        dtcFechaFin.setDate(null);        
    } 
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnGenerarPDF;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbCiudad;
    private javax.swing.JComboBox cmbContinente;
    private javax.swing.JComboBox cmbPais;
    private com.toedter.calendar.JDateChooser dtcFechaFin;
    private com.toedter.calendar.JDateChooser dtcFechaInicio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnKardex;
    private javax.swing.JTable tblResultados;
    // End of variables declaration//GEN-END:variables
}
